package com.itheima.d6_map_traverse;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class MapTest4 {
    public static void main(String[] args) {
 // 创建Scanner对象，用于读取用户输入
        Scanner scanner = new Scanner(System.in);

        // 提示用户输入字符串
        System.out.println("请输入一个字符串: ");
        String input = scanner.next();

        // 创建HashMap来存储字符及其出现次数
        HashMap<Character, Integer> charCountMap = new HashMap<>();

        // 遍历字符串，统计每个字符的出现次数
        char[] charArray = input.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (charCountMap.containsKey(c)){
                Integer valur = charCountMap.get(c);
                charCountMap.put(c,valur+1);
            }else{
                charCountMap.put(c,1);
            }

        }


        // 遍历HashMap，输出每个字符及其出现次数
        Set<Map.Entry<Character, Integer>> entries = charCountMap.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            System.out.println("字符: " + entry.getKey() + ", 出现次数: " + entry.getValue());

        }

    }
}

